package com.koron.order.prodForm.service.impl;


import com.github.pagehelper.PageInfo;
import com.koron.bean.query.PageQuery;
import com.koron.bean.system.org.dto.OrgDto;
import com.koron.bean.system.org.query.OrgQuery;
import com.koron.bean.system.role.RoleBean;
import com.koron.bean.system.staff.vo.EamUser;
import com.koron.common.core.config.ThreadLocalContext;
import com.koron.order.prodForm.bean.po.ProdForm;
import com.koron.order.prodForm.bean.query.ProdFormQuery;
import com.koron.order.prodForm.mapper.ProdFormMapper;
import com.koron.order.prodForm.service.ProdFormService;
import org.koron.ebs.mybatis.SessionFactory;
import org.koron.ebs.mybatis.TaskAnnotation;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.*;

/**
 * 生产报表(ProdForm)表服务实现类
 *
 * @author jzp
 * @since 2022-08-10 15:27:52
 */
@Service
public class ProdFormServiceImpl implements ProdFormService {

    /**
     * 通过ID查询单条数据
     *
     */
    @Override
    @TaskAnnotation("getById")
    public ProdForm getById(SessionFactory factory, String id) {
        ProdFormMapper mapper = factory.getMapper(ProdFormMapper.class);
        return mapper.selectById(id);
    }

    /**
     * 查询所有数据
     *
     */
    @Override
    @TaskAnnotation("list")
    public List<ProdForm> list(SessionFactory factory, ProdFormQuery query) {
        ProdFormMapper mapper = factory.getMapper(ProdFormMapper.class);
        return mapper.selectList(query);
    }

    @Override
    @TaskAnnotation("page")
    public PageInfo<ProdForm> page(SessionFactory factory, PageQuery pageQuery, ProdFormQuery query) {
        return ProdFormService.super.page(factory, pageQuery, query);
    }

    @Override
    public void addAuthQuery(SessionFactory factory, ProdFormQuery query) {
        EamUser eamUser = ThreadLocalContext.get();
        if (eamUser != null){
            Optional<RoleBean> optional = Optional.ofNullable(eamUser)
                    .map(eamUser1 ->
                            eamUser.getUserOwnJurisdictionBean()
                                    .getRoleBean()
                                    .stream().filter(roleBean -> roleBean.getRoleType().equals("role_root") && roleBean.getAppid().equals(eamUser.getAppId()))
                                    .findFirst())
                    .orElse(null);
            if (optional.isPresent()){
                return;
            }
//            Map <String,OrgDto> map = queryOrg();
//            OrgDto orgDto = map.get(eamUser.getOrgId());
//            if (orgDto != null){
//                List<String> orgIds = orgDto.getOrgIds();
//                query.setDeptIdList(orgIds);
//            }
            List<String> allOrgIds = getCurrUserOrgIds();
            if (!CollectionUtils.isEmpty(allOrgIds)){
                query.setDeptIdList(allOrgIds);
            }
        }

    }

    private Map<String,OrgDto> queryOrg() {
        SessionFactory factory = new SessionFactory();
        EamUser eamUser = ThreadLocalContext.get();
        factory.setDbEnv(eamUser.getCurrDs());
        OrgQuery orgQuery = new OrgQuery();
        orgQuery.setAppid(eamUser.getAppId());
        ProdFormMapper mapper = factory.getMapper(ProdFormMapper.class);
        List<OrgDto> orgTree = mapper.queryOrgTree(orgQuery);
        return buildTree(orgTree);
    }

    private Map buildTree(List<OrgDto> orgTree) {
        Map<String, OrgDto> map = new HashMap<>();
        for (OrgDto orgDto : orgTree) {
            map.put(orgDto.getId(), orgDto);
        }

        for (OrgDto orgDto : orgTree) {
            String pId = orgDto.getParent();
            OrgDto parent = map.get(pId);
            if (parent != null){
                if (CollectionUtils.isEmpty(parent.getOrgIds())){
                    parent.setOrgIds(new ArrayList<>());
                }
                parent.getOrgIds().add(orgDto.getId());
            }
        }
        return map;
    }

    /**
     * 新增数据
     *
     */
    @Override
    @TaskAnnotation("save")
    public boolean save(SessionFactory factory, ProdForm prodForm) {
        ProdFormMapper mapper = factory.getMapper(ProdFormMapper.class);
        return mapper.insertOrUpdate(prodForm) > 0;
    }

    /**
     * 修改数据
     *
     */
    @Override
    @TaskAnnotation("update")
    public boolean update(SessionFactory factory, ProdForm prodForm) {
        ProdFormMapper mapper = factory.getMapper(ProdFormMapper.class);
        return mapper.update(prodForm) > 0;
    }

    /**
     * 通过主键删除数据
     *
     */
    @Override
    @TaskAnnotation("deleteById")
    public boolean deleteById(SessionFactory factory, String id) {
        ProdFormMapper mapper = factory.getMapper(ProdFormMapper.class);
        return mapper.hidden(id) > 0;
    }
}
